package com.mywang.weitravel.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.mywang.weitravel.R;
import com.mywang.weitravel.model.FindListBean;
import com.mywang.weitravel.modules.image.GlideImageEngine;
import com.mywang.weitravel.util.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

public class FindListAdapter extends RecyclerView.Adapter<FindListAdapter.FindListHolder> {

    private Context mContext;

    private List<FindListBean.DataBean> mDatas = new ArrayList<>();

    private final int IMAGE_WIDTH = (ScreenUtil.getScreenWidth() - ScreenUtil.dp2px(40)) / 2;

    public FindListAdapter(Context context) {
        this.mContext = context;
    }

    public void addData(List<FindListBean.DataBean> listBeans) {
        mDatas.addAll(listBeans);
        notifyItemInserted(mDatas.size());
    }

    public void refreshData(List<FindListBean.DataBean> listBeans) {
        mDatas.clear();
        mDatas.addAll(listBeans);
        notifyItemRangeChanged(0, mDatas.size());
    }

    public void collect(String findId) {
        for (FindListBean.DataBean data : mDatas) {
            if (findId.equals(data.getId())) {
                data.setIs_support(1);
                notifyDataSetChanged();
                break;
            }
        }
    }


    @Override
    public FindListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_find_list, parent, false);
        return new FindListHolder(view);
    }

    @Override
    public void onBindViewHolder(FindListHolder holder, int position) {
        FindListBean.DataBean dataBean = mDatas.get(position);
        if (position == 0) {
            holder.ivPicture.getLayoutParams().height = (int) (IMAGE_WIDTH * 133 / 160f);
        } else {
            holder.ivPicture.getLayoutParams().height = (int) (IMAGE_WIDTH * 153 / 160f);
        }
        GlideImageEngine.getInstance().loadImage(dataBean.getUrl(), holder.ivPicture);
        GlideImageEngine.getInstance().loadImage(dataBean.getAvatar(), holder.ivAvatar, R.mipmap.icon_head_default, ScreenUtil.dp2px(25));
        if (dataBean.getNickname() != null) {
            holder.tvUsername.setText(dataBean.getNickname());
        } else if (dataBean.getUsername() != null) {
            holder.tvUsername.setText(dataBean.getUsername());
        }
        holder.tvTitle.setText(dataBean.getTitle());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(dataBean, position);
                }
            }
        });
        holder.ivCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.collect(dataBean);
                }
            }
        });
        if (dataBean.isSupport()) {
            holder.ivCollect.setImageResource(R.mipmap.icon_mine_collection);
        } else {
            holder.ivCollect.setImageResource(R.mipmap.follow);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class FindListHolder extends RecyclerView.ViewHolder {

        ImageView ivPicture;
        ImageView ivCollect;
        ImageView ivAvatar;
        TextView tvTitle;
        TextView tvUsername;

        public FindListHolder(View itemView) {
            super(itemView);
            ivPicture = itemView.findViewById(R.id.iv_picture);
            ivAvatar = itemView.findViewById(R.id.iv_avatar);
            ivCollect = itemView.findViewById(R.id.iv_collect);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvUsername = itemView.findViewById(R.id.tv_user_name);
        }
    }

    private OnItemOnclickListener listener;

    public void setOnItemOnclickListener(OnItemOnclickListener listener) {
        this.listener = listener;
    }

    public interface OnItemOnclickListener {
        void onItemClick(FindListBean.DataBean data, int position);

        void collect(FindListBean.DataBean data);
    }
}
